﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.NhanVienModel;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.NhanVien;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.NhanVienDelegate;
using Webdiyer.WebControls.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.ChamCongModel;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.Controllers
{
    public class NhanVienController : Controller
    {
        //
        // GET: /NhanVien/
        #region Nhân viên


        NhanVienDelegate _NhanVienDelegate = new NhanVienDelegate();
        public ActionResult DanhSachNhanVien(int? id, string SearchObject, string SearchInput)
        {

            IQueryable<hs_NhanVien> lst = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lst = _NhanVienDelegate.QuickSearch(id, SearchObject, SearchInput);
            else
                lst = _NhanVienDelegate.GetAll(false, false);
            NhanVienForm form = new NhanVienForm();
            if (lst != null)
            {
                //form.BenVayPagedList = new Webdiyer.WebControls.Mvc.PagedList<Models.LinQ.NghiepVu.hs_BenVay>(lst.ToList(), id ?? 1
                //    , Common.CommonVariables.PaggedItemsPerPage);
                form.NhanVienPagedList = lst.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
            }
            else
                form.NhanVienPagedList = new Webdiyer.WebControls.Mvc.PagedList<hs_NhanVien>(new List<hs_NhanVien>(), 1, 1);

            form.Pagenumber = id ?? 1;
            form.SearchInput = SearchInput;
            form.SearchObject = SearchObject;

            return View("~/Views/QuanLyNhanVien/DanhSachNhanVien.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ThemMoiNhanVien(int id, string Status)
        {
            NhanVienForm form = new NhanVienForm();
            if (id > 0)
            {
                hs_NhanVien obj = _NhanVienDelegate.GetById(id);
                form = _NhanVienDelegate.ConvertToForm(obj);
                if (Status == "1")
                {
                    form.Message = Common.CommonMessage.MessageInsertSuccess;
                }
                else
                {
                    form.MessageError = Common.CommonMessage.MessageInsertError;
                }

            }
            else
            {
                form.Id = id;
                // form.SoBienNhan = _BenVayDelegate.GetSoBienNhan();
            }
            form.CbChucVu = _NhanVienDelegate.BuildChucVu(0);
            form.CbDanToc = _NhanVienDelegate.BuildDanToc(0);
            form.CbTonGiao = _NhanVienDelegate.BuildTonGiao(0);
            form.CbTinh = _NhanVienDelegate.BuildTinh(0);


            return View("~/Views/QuanLyNhanVien/ThemMoiNhanVien.aspx", form);
        }
        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult ThemMoiNhanVien(int id, NhanVienForm form)
        {
            hs_NhanVien Status;
            if (id == -1)// thêm mới
            {
                hs_NhanVien obj = _NhanVienDelegate.ConvertToLinq(form);
                Status = _NhanVienDelegate.Insert(obj);
                if (Status != null)
                {
                    form.CbChucVu = _NhanVienDelegate.BuildChucVu(Status.IdChucVu.Value);
                    form.CbDanToc = _NhanVienDelegate.BuildDanToc(Status.IdDanToc.Value);
                    form.CbTonGiao = _NhanVienDelegate.BuildTonGiao(Status.IdTonGiao.Value);
                    form.CbTinh = _NhanVienDelegate.BuildTinh(Status.IdTinh.Value);
                }
                return Redirect("~/NhanVien/ThemMoiNhanVien/" + Status.Id + "?Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThemMoiNhanVien/" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult SuaNhanVien(int id)
        {
            NhanVienForm form = new NhanVienForm();
            hs_NhanVien obj = _NhanVienDelegate.GetById(id);
            form = _NhanVienDelegate.ConvertToForm(obj);
            hs_NhanVien Status = _NhanVienDelegate.GetById(id);
            form.CbChucVu = _NhanVienDelegate.BuildChucVu(Status.IdChucVu.Value);
            form.CbDanToc = _NhanVienDelegate.BuildDanToc(Status.IdDanToc.Value);
            form.CbTonGiao = _NhanVienDelegate.BuildTonGiao(Status.IdTonGiao.Value);
            form.CbTinh = _NhanVienDelegate.BuildTinh(Status.IdTinh.Value);
            return View("~/Views/QuanLyNhanVien/ThemMoiNhanVien.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaNhanVien(int id, NhanVienForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            hs_NhanVien obj = _NhanVienDelegate.ConvertToLinq(form);
            obj.Id = id;
            bool result = _NhanVienDelegate.UpdateNhanVien(obj);
            if (result)
            {
                form.Message = Common.CommonMessage.MessageUpdateSuccess;
            }
            else
                form.MessageError = Common.CommonMessage.MessageUpdateError;
            hs_NhanVien Status = _NhanVienDelegate.GetById(id);
            form.CbChucVu = _NhanVienDelegate.BuildChucVu(Status.IdChucVu.Value);
            form.CbDanToc = _NhanVienDelegate.BuildDanToc(Status.IdDanToc.Value);
            form.CbTonGiao = _NhanVienDelegate.BuildTonGiao(Status.IdTonGiao.Value);
            form.CbTinh = _NhanVienDelegate.BuildTinh(Status.IdTinh.Value);
            return View("~/Views/QuanLyNhanVien/ThemMoiNhanVien.aspx", form);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }
        #endregion
        #region Xe Vận Tải
        XeVanTaiDelegate _XeVanTaiDelegate = new XeVanTaiDelegate();
        public ActionResult DanhSachXeVanTai(int? id, string SearchObject, string SearchInput)
        {
            IQueryable<pt_XeVanTai> lst = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lst = _XeVanTaiDelegate.QuickSearch(id, SearchObject, SearchInput);
            else
                lst = _XeVanTaiDelegate.GetAll();
            XeVanTaiForm form = new XeVanTaiForm();
            if (lst != null)
            {
                //form.BenVayPagedList = new Webdiyer.WebControls.Mvc.PagedList<Models.LinQ.NghiepVu.hs_BenVay>(lst.ToList(), id ?? 1
                //    , Common.CommonVariables.PaggedItemsPerPage);
                form.XeVanTaiPagedList = lst.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
            }
            else
                form.XeVanTaiPagedList = new Webdiyer.WebControls.Mvc.PagedList<pt_XeVanTai>(new List<pt_XeVanTai>(), 1, 1);

            form.Pagenumber = id ?? 1;
            form.SearchInput = SearchInput;
            form.SearchObject = SearchObject;
            return View("~/Views/QuanLyNhanVien/DanhSachXeVanTai.aspx", form);
        }

        public ActionResult DanhSachLichSuNgayDK(int? idPhanTrang, int id)
        {
            pt_XeVanTai obj = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());

            IQueryable<Pt_XeVanTai_NgayKiemDinhXe> lst = null;
            XeVT_NgayDKDelegate _xeVtNgayDkDelegate = new XeVT_NgayDKDelegate();
            lst = _xeVtNgayDkDelegate.GetAll(id);
            XeVanTaiForm form = new XeVanTaiForm();
            form = _XeVanTaiDelegate.ConvertToForm(obj);
            if (lst != null)
            {
                //form.BenVayPagedList = new Webdiyer.WebControls.Mvc.PagedList<Models.LinQ.NghiepVu.hs_BenVay>(lst.ToList(), id ?? 1
                //    , Common.CommonVariables.PaggedItemsPerPage);

                PagedList<Pt_XeVanTai_NgayKiemDinhXe> list = lst.ToPagedList(idPhanTrang ?? 1, CommonVariables.PaggedItemsPerPage);
                form.PtXeVanTaiNgayKiemDinhPagedList = list;
            }
            else
                form.PtXeVanTaiNgayKiemDinhPagedList = new Webdiyer.WebControls.Mvc.PagedList<Pt_XeVanTai_NgayKiemDinhXe>(new List<Pt_XeVanTai_NgayKiemDinhXe>(), 1, 1);
            form.Pagenumber = idPhanTrang ?? 1;

            return View("~/Views/QuanLyNhanVien/DanhSachNgayDK.aspx", form);
        }
        public ActionResult DanhSachLichSuNgayBH(int? idPhanTrang, int id)
        {
            pt_XeVanTai obj = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());

            IQueryable<pt_XeVanTai_NgayBaoHiemXe> lst = null;
            XeVT_NgayBHDelegate _xeVtNgayDhDelegate = new XeVT_NgayBHDelegate();
            lst = _xeVtNgayDhDelegate.GetAll(id);
            XeVanTaiForm form = new XeVanTaiForm();
            form = _XeVanTaiDelegate.ConvertToForm(obj);
            if (lst != null)
            {
                //form.BenVayPagedList = new Webdiyer.WebControls.Mvc.PagedList<Models.LinQ.NghiepVu.hs_BenVay>(lst.ToList(), id ?? 1
                //    , Common.CommonVariables.PaggedItemsPerPage);

                PagedList<pt_XeVanTai_NgayBaoHiemXe> list = lst.ToPagedList(idPhanTrang ?? 1, CommonVariables.PaggedItemsPerPage);
                form.PtXeVanTaiNgayBaoHiemPagedList = list;
            }
            else
                form.PtXeVanTaiNgayBaoHiemPagedList = new Webdiyer.WebControls.Mvc.PagedList<pt_XeVanTai_NgayBaoHiemXe>(new List<pt_XeVanTai_NgayBaoHiemXe>(), 1, 1);
            form.Pagenumber = idPhanTrang ?? 1;

            return View("~/Views/QuanLyNhanVien/DanhSachNgayBH.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult ThemMoiXeVanTai(int id, string Status)
        {
            XeVanTaiForm form = new XeVanTaiForm();
            if (id > 0)
            {
                pt_XeVanTai obj = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());
                form = _XeVanTaiDelegate.ConvertToForm(obj);
                if (Status == "1")
                {
                    form.Message = Common.CommonMessage.MessageInsertSuccess;
                }
                else
                {
                    form.MessageError = Common.CommonMessage.MessageInsertError;
                }
                form.Kt = true;
            }
            else
            {
                form.Id = id;
                form.Kt = false;
                // form.SoBienNhan = _BenVayDelegate.GetSoBienNhan();
            }
            form.CbThungXe = _XeVanTaiDelegate.BuildThungXe(0);


            return View("~/Views/QuanLyNhanVien/ThemMoiXeVanTai.aspx", form);
        }

        public ActionResult DanhSachXeNopTienToiHan()
        {
            DateTime dateTime = DateTime.Now;
            XeVanTaiDelegate _xeVanTaiDelegate = new XeVanTaiDelegate();
            List<pt_XeVanTai> list = _xeVanTaiDelegate.GetALLXeVanTai();
            string t = "";
            List<pt_XeVanTai> obj = new List<pt_XeVanTai>();
            foreach (var ptXeVanTai in list)
            {

                if (dateTime.AddDays(30) >= ptXeVanTai.HanKiemDinhXe.Value && dateTime.AddDays(30) >= ptXeVanTai.HetHanBaoHiem.Value)
                {
                    obj.Add(ptXeVanTai);
                }
                else if (dateTime.AddDays(30) >= ptXeVanTai.HanKiemDinhXe.Value && dateTime.AddDays(30) < ptXeVanTai.HetHanBaoHiem.Value)
                {
                    obj.Add(ptXeVanTai);
                }
                else if (dateTime.AddDays(30) < ptXeVanTai.HanKiemDinhXe.Value && dateTime.AddDays(30) >= ptXeVanTai.HetHanBaoHiem.Value)
                {
                    obj.Add(ptXeVanTai);
                }

            }
            XeVanTaiForm form = new XeVanTaiForm();
            form.ListPhaiDong = obj;
            //     Session["ThongBao"] = t.Remove(t.Length - 1);

            return View("~/Views/QuanLyNhanVien/DanhSachCacXePhaiDong.aspx", form);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemMoiXeVanTai(int id, XeVanTaiForm form)
        {
            pt_XeVanTai Status;
            if (id == -1)// thêm mới
            {
                pt_XeVanTai obj = _XeVanTaiDelegate.ConvertToLinq(form);
                Status = _XeVanTaiDelegate.Insert(obj);
                if (Status != null)
                {

                    form.CbThungXe = _XeVanTaiDelegate.BuildThungXe(Status.IdDangThungXe.Value);

                }
                return Redirect("~/NhanVien/ThemMoiXeVanTai/" + Status.Id + "?Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThemMoiXeVanTai/" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult CapNhatLichSuNgayThang(int id, XeVanTaiForm form)
        {
            Pt_XeVanTai_NgayKiemDinhXe ptXeVanTaiNgayKiemDinhXe = new Pt_XeVanTai_NgayKiemDinhXe();
            ptXeVanTaiNgayKiemDinhXe.NgayKiemDinh = Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayDK);
            ptXeVanTaiNgayKiemDinhXe.IdXeVanTai = id;
            XeVT_NgayDKDelegate xeVtNgayDkDelegate = new XeVT_NgayDKDelegate();
            xeVtNgayDkDelegate.Insert(ptXeVanTaiNgayKiemDinhXe);

            pt_XeVanTai_NgayBaoHiemXe ptXeVanTaiNgayBaoHiemXe = new pt_XeVanTai_NgayBaoHiemXe();
            ptXeVanTaiNgayBaoHiemXe.NgayBaoHiem = Common.DateTimeExtension.ConvertStringDDMMYYYYToYYYYMMDD(form.NgayBH);
            ptXeVanTaiNgayBaoHiemXe.IdXeVanTai = id;
            XeVT_NgayBHDelegate xeVtNgayBhDelegate = new XeVT_NgayBHDelegate();
            xeVtNgayBhDelegate.Insert(ptXeVanTaiNgayBaoHiemXe);

            pt_XeVanTai obj = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());
            form.CbThungXe = _XeVanTaiDelegate.BuildThungXe(obj.IdDangThungXe.Value);
            return Redirect("~/NhanVien/ThemMoiXeVanTai/" + id + "?Status=" + 1);
        }




        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult SuaXeVanTai(int id)
        {
            XeVanTaiForm form = new XeVanTaiForm();
            pt_XeVanTai obj = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());
            form = _XeVanTaiDelegate.ConvertToForm(obj);
            pt_XeVanTai Status = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());
            form.CbThungXe = _XeVanTaiDelegate.BuildThungXe(Status.IdDangThungXe.Value);

            return View("~/Views/QuanLyNhanVien/ThemMoiXeVanTai.aspx", form);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaXeVanTai(int id, XeVanTaiForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            pt_XeVanTai obj = _XeVanTaiDelegate.ConvertToLinq(form);
            obj.Id = id;
            bool result = _XeVanTaiDelegate.UpdateXeVanTai(obj);
            if (result)
            {
                form.Message = Common.CommonMessage.MessageUpdateSuccess;
            }
            else
                form.MessageError = Common.CommonMessage.MessageUpdateError;
            pt_XeVanTai Status = _XeVanTaiDelegate.GetXeVanTaiById(id.ToString());
            form.CbThungXe = _XeVanTaiDelegate.BuildThungXe(Status.IdDangThungXe.Value);

            return View("~/Views/QuanLyNhanVien/ThemMoiXeVanTai.aspx", form);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }

        #endregion
        // lương cơ bản
        #region Thông tin chi tiêt nhân viên
        public ActionResult ThongTinChiTiet(int Id, int HidLastTab, int Status, int? idPTLuongCoBan, int? idPTTrachNhiem,
            int? idPTPCDiLai, int? idPTPCCom, int? idPTPCDienThoai, int? idPTPCChuyenCan, string SearchObject, string SearchInput)
        {
            //lương cơ bản
            #region show lương cơ bản

            IQueryable<hs_NhanVien_LuongCoBan> lst = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lst = _luongCoBanDelegate.QuickSearch(Id, idPTLuongCoBan, SearchObject, SearchInput);
            else
                lst = _luongCoBanDelegate.GetAll(Id);
            NhanVienForm form = new NhanVienForm();
            if (lst != null)
            {
                PagedList<hs_NhanVien_LuongCoBan> list = lst.ToPagedList(idPTLuongCoBan ?? 1, CommonVariables.PaggedItemsPerPage);
                form.ModelLuongCoBan.LuongCoBanNhanVienPagedList = list;
            }
            else
                form.ModelLuongCoBan.LuongCoBanNhanVienPagedList = new PagedList<hs_NhanVien_LuongCoBan>(new List<hs_NhanVien_LuongCoBan>(), 1, 1);
            form.ModelLuongCoBan.CbLuongCoBan = _luongCoBanDelegate.BuildLuongCoBan(0);
            form.ModelLuongCoBan.Pagenumber = idPTLuongCoBan ?? 1;
            #endregion
            // trách nhiệm
            #region show Trách nhiệm



            IQueryable<hs_NhanVien_TrachNhiem> lstTrachNhiem = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lstTrachNhiem = _TrachNhiemDelegate.QuickSearch(Id, idPTTrachNhiem, SearchObject, SearchInput);
            else
                lstTrachNhiem = _TrachNhiemDelegate.GetAll(Id);

            if (lst != null)
            {
                PagedList<hs_NhanVien_TrachNhiem> list = lstTrachNhiem.ToPagedList(idPTTrachNhiem ?? 1, CommonVariables.PaggedItemsPerPage);
                form.ModelTrachNhiem.TrachNhiemNhanVienPagedList = list;
            }
            else
                form.ModelTrachNhiem.TrachNhiemNhanVienPagedList = new PagedList<hs_NhanVien_TrachNhiem>(new List<hs_NhanVien_TrachNhiem>(), 1, 1);
            form.ModelTrachNhiem.CbTrachNhiem = _TrachNhiemDelegate.BuildTrachNhiem(0);
            form.ModelTrachNhiem.Pagenumber = idPTTrachNhiem ?? 1;
            #endregion
            //Phụ cấp đi lại
            #region show Phụ cấp đi lại

            IQueryable<hs_NhanVien_PCDiLai> lstPCDiLai = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lstPCDiLai = _PCDiLaiDelegate.QuickSearch(Id, idPTPCDiLai, SearchObject, SearchInput);
            else
                lstPCDiLai = _PCDiLaiDelegate.GetAll(Id);
            if (lst != null)
            {
                PagedList<hs_NhanVien_PCDiLai> list = lstPCDiLai.ToPagedList(idPTPCDiLai ?? 1, CommonVariables.PaggedItemsPerPage);
                form.ModelPCDiLai.PCDiLaiNhanVienPagedList = list;
            }
            else
                form.ModelPCDiLai.PCDiLaiNhanVienPagedList = new PagedList<hs_NhanVien_PCDiLai>(new List<hs_NhanVien_PCDiLai>(), 1, 1);
            form.ModelPCDiLai.CbPCDiLai = _PCDiLaiDelegate.BuildPCDiLai(0);
            form.ModelPCDiLai.Pagenumber = idPTPCDiLai ?? 1;
            #endregion

            //Phụ cấp cơm
            #region show Phụ cấp cơm
            PCComDelegate _PCComDelegate = new PCComDelegate();
            IQueryable<hs_NhanVien_PCCom> lstPCCom = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lstPCCom = _PCComDelegate.QuickSearch(Id, idPTPCCom, SearchObject, SearchInput);
            else
                lstPCCom = _PCComDelegate.GetAll(Id);
            if (lst != null)
            {
                PagedList<hs_NhanVien_PCCom> list = lstPCCom.ToPagedList(idPTPCCom ?? 1, CommonVariables.PaggedItemsPerPage);
                form.ModelPCCom.PCComNhanVienPagedList = list;
            }
            else
                form.ModelPCCom.PCComNhanVienPagedList = new PagedList<hs_NhanVien_PCCom>(new List<hs_NhanVien_PCCom>(), 1, 1);
            form.ModelPCCom.CbPCCom = _PCComDelegate.BuildPCCom(0);
            form.ModelPCCom.Pagenumber = idPTPCCom ?? 1;
            #endregion

            //Phụ cấp điện thoại
            #region show Phụ điện thoại
            PCDienThoaiDelegate _PCDienThoaiDelegate = new PCDienThoaiDelegate();
            IQueryable<hs_NhanVien_PCDienThoai> lstPCDienThoai = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lstPCDienThoai = _PCDienThoaiDelegate.QuickSearch(Id, idPTPCDienThoai, SearchObject, SearchInput);
            else
                lstPCDienThoai = _PCDienThoaiDelegate.GetAll(Id);
            if (lst != null)
            {
                PagedList<hs_NhanVien_PCDienThoai> list = lstPCDienThoai.ToPagedList(idPTPCDienThoai ?? 1, CommonVariables.PaggedItemsPerPage);
                form.ModelPCDienThoai.PCDienThoaiNhanVienPagedList = list;
            }
            else
                form.ModelPCDienThoai.PCDienThoaiNhanVienPagedList = new PagedList<hs_NhanVien_PCDienThoai>(new List<hs_NhanVien_PCDienThoai>(), 1, 1);
            form.ModelPCDienThoai.CbPCDienThoai = _PCDienThoaiDelegate.BuildPCDienThoai(0);
            form.ModelPCDienThoai.Pagenumber = idPTPCDienThoai ?? 1;
            #endregion


            //Phụ cấp Chuyên Cần
            #region show Phụ chuyên cần
            PCChuyenCanDelegate _PCChuyenCanDelegate = new PCChuyenCanDelegate();
            IQueryable<hs_NhanVien_PCChuyenCan> lstPCChuyenCan = null;
            if (!string.IsNullOrEmpty(SearchInput))
                lstPCChuyenCan = _PCChuyenCanDelegate.QuickSearch(Id, idPTPCChuyenCan, SearchObject, SearchInput);
            else
                lstPCChuyenCan = _PCChuyenCanDelegate.GetAll(Id);
            if (lst != null)
            {
                PagedList<hs_NhanVien_PCChuyenCan> list = lstPCChuyenCan.ToPagedList(idPTPCChuyenCan ?? 1, CommonVariables.PaggedItemsPerPage);
                form.ModelPCChuyenCan.PCChuyenCanNhanVienPagedList = list;
            }
            else
                form.ModelPCChuyenCan.PCChuyenCanNhanVienPagedList = new PagedList<hs_NhanVien_PCChuyenCan>(new List<hs_NhanVien_PCChuyenCan>(), 1, 1);
            form.ModelPCChuyenCan.CbPCChuyenCan = _PCChuyenCanDelegate.BuildPCChuyenCan(0);
            form.ModelPCChuyenCan.Pagenumber = idPTPCChuyenCan ?? 1;
            #endregion
            // thông tin nhân viên
            #region show Thông tin Nhân viên


            hs_NhanVien obj = _NhanVienDelegate.GetById(Id);
            form.Ho = obj.Ho;
            form.Ten = obj.Ten;
            form.NamSinh = obj.NamSinh;
            form.SoCMND = obj.SoCMND;
            form.SoThe = obj.SoThe;
            form.Id = obj.Id;
            #endregion

            // thông tin liên quan
            #region show Thông tin liên quan

            ThongTinLienQuanDelegate _thongTinLienQuanDelegate = new ThongTinLienQuanDelegate();
            hs_ThongTinLienQuan objLienQuan = _thongTinLienQuanDelegate.GetThogntinLienQuanById(Id.ToString());
            try
            {
                form.ModelThongTinLienQuan.NgayNhanViecTheoHopDong = objLienQuan.NgayNhanViecTheoHopDong;
            }
            catch (Exception)
            {

                form.ModelThongTinLienQuan.NgayNhanViecTheoHopDong = null;
            }
            try
            {
                form.ModelThongTinLienQuan.NgayThuViec = objLienQuan.NgayThuViec;
            }
            catch (Exception)
            {

                form.ModelThongTinLienQuan.NgayThuViec = null;
            }
            try
            {
                form.ModelThongTinLienQuan.HopDong6Thang = objLienQuan.HopDong6Thang;
            }
            catch (Exception)
            {

                form.ModelThongTinLienQuan.HopDong6Thang = null;
            }
            try
            {
                form.ModelThongTinLienQuan.HopDong12Thang = objLienQuan.HopDong12Thang;
            }
            catch (Exception)
            {

                form.ModelThongTinLienQuan.HopDong12Thang = null;
            }
            try
            {
                form.ModelThongTinLienQuan.IdKeyHSLienQuan = objLienQuan.Id;
            }
            catch (Exception)
            {

                form.ModelThongTinLienQuan.IdKeyHSLienQuan = 0;
            }

            //form.ModelThongTinLienQuan.NgayNhanViecTheoHopDong = objLienQuan.NgayNhanViecTheoHopDong == null ? DateTime.Today : objLienQuan.NgayNhanViecTheoHopDong;
            //form.ModelThongTinLienQuan.NgayThuViec = objLienQuan.NgayThuViec == null ? DateTime.Today : objLienQuan.NgayThuViec;
            //form.ModelThongTinLienQuan.HopDong6Thang = objLienQuan.HopDong6Thang == null ? DateTime.Today : objLienQuan.HopDong6Thang;
            //form.ModelThongTinLienQuan.HopDong12Thang = objLienQuan.HopDong12Thang;
            form.Id = obj.Id;
            #endregion
            //Thông báo
            #region Thông báo view
            form.HidLastTab = HidLastTab;
            if (Status == 1)
            {
                form.Message = "Thành Công";
            }
            else if (Status == 0)
            {
                form.Message = "Thất bại";
            }
            else
            {
                form.MessageError = "";
            }
            #endregion

            return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", form);
        }
        #endregion


        #region Lương cơ bản
        LuongCoBanDelegate _luongCoBanDelegate = new LuongCoBanDelegate();


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemLuongCoBan(int IdNhanVien, int IdMucLuong, int? idPTLuongCoBan, LuongCoBanForm form)
        {
            hs_NhanVien_LuongCoBan Status;
            if (IdMucLuong == 0)// thêm mới
            {
                hs_NhanVien_LuongCoBan obj = _luongCoBanDelegate.ConvertToLinq(form);
                obj.IdNhanVien = IdNhanVien;
                // obj.IdLuongCoBan = form.ModelLuongCoBan.IdLuongCoBan;
                Status = _luongCoBanDelegate.Insert(obj);
                NhanVienForm fromnv = new NhanVienForm();
                if (Status != null)
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 1);
                }
                else
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 0);
                }
                //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaLuongCoBan(int IdNhanVien, int IdMucLuong, int? idPTLuongCoBan, LuongCoBanForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            NhanVienForm fromnv = new NhanVienForm();
            hs_NhanVien_LuongCoBan obj = _luongCoBanDelegate.ConvertToLinq(form);
            obj.IdNhanVien = IdNhanVien;
            obj.Id = IdMucLuong;
            bool result = _luongCoBanDelegate.UpdateLuongCoBan(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult XoaLuongCoBan(int IdNhanVien, int IdMucLuong)
        {
            bool t = _luongCoBanDelegate.DeleteLuongCoBan(IdMucLuong);
            NhanVienForm fromnv = new NhanVienForm();
            if (t == true)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 0);
            }
        }


        public JsonResult EditLuongCoBanAjax(int IdLuongCoBan)// IdThongTinNguonThu
        {
            hs_NhanVien_LuongCoBan obj = _luongCoBanDelegate.GetLuongCoBanById(IdLuongCoBan.ToString());
            string[] kq = new string[] { obj.Id.ToString(),
                obj.IdLuongCoBan!=null? obj.IdLuongCoBan.Value.ToString():""};
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

        public ActionResult CapNhatTrangThaiSuDung(int IdNhanVien, int IdKeyLuongCoBan)
        {
            if (_luongCoBanDelegate.UpdateTrangThaiSuDung(IdKeyLuongCoBan) == true)
            {
                _luongCoBanDelegate.UpdateTrangThaiKhongSuDung(IdNhanVien, IdKeyLuongCoBan);
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=1&&Status=" + 0);
            }
        }
        #endregion
        // trách nhiệm
        #region Trách nhiệm
        TrachNhiemDelegate _TrachNhiemDelegate = new TrachNhiemDelegate();
        public ActionResult CapNhatTrangThaiSuDungTrachNhiem(int IdNhanVien, int IdTrachNhiem)
        {
            if (_TrachNhiemDelegate.UpdateTrangThaiSuDung(IdTrachNhiem) == true)
            {
                _TrachNhiemDelegate.UpdateTrangThaiKhongSuDung(IdNhanVien, IdTrachNhiem);
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 0);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemTrachNhiem(int IdNhanVien, int IdKeyTrachNhiem, int? idPTTrachNhiem, TrachNhiemForm form)
        {
            hs_NhanVien_TrachNhiem Status;
            if (IdKeyTrachNhiem == 0)// thêm mới
            {
                hs_NhanVien_TrachNhiem obj = _TrachNhiemDelegate.ConvertToLinq(form);
                obj.IdNhanVien = IdNhanVien;
                // obj.IdKeyTrachNhiem = form.ModelTrachNhiem.IdKeyTrachNhiem;
                Status = _TrachNhiemDelegate.Insert(obj);
                NhanVienForm fromnv = new NhanVienForm();
                if (Status != null)
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 1);
                }
                else
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 0);
                }
                //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaTrachNhiem(int IdNhanVien, int IdKeyTrachNhiem, int? idPTTrachNhiem, TrachNhiemForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            NhanVienForm fromnv = new NhanVienForm();
            hs_NhanVien_TrachNhiem obj = _TrachNhiemDelegate.ConvertToLinq(form);
            obj.IdNhanVien = IdNhanVien;
            obj.Id = IdKeyTrachNhiem;
            bool result = _TrachNhiemDelegate.UpdateTrachNhiem(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult XoaTrachNhiem(int IdNhanVien, int IdKeyTrachNhiem)
        {
            bool t = _TrachNhiemDelegate.DeleteTrachNhiem(IdKeyTrachNhiem);
            NhanVienForm fromnv = new NhanVienForm();
            if (t == true)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=2&&Status=" + 0);
            }
        }


        public JsonResult EditTrachNhiemAjax(int IdKeyTrachNhiem)// IdThongTinNguonThu
        {
            hs_NhanVien_TrachNhiem obj = _TrachNhiemDelegate.GetTrachNhiemById(IdKeyTrachNhiem.ToString());
            string[] kq = new string[] { obj.Id.ToString(),
                obj.IdTrachNhiem!=null? obj.IdTrachNhiem.Value.ToString():""};
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion
        // phụ cấp đi lại
        #region Phụ cấp đi lại
        PCDiLaiDelegate _PCDiLaiDelegate = new PCDiLaiDelegate();
        public ActionResult CapNhatTrangThaiSuDungPCDiLai(int IdNhanVien, int IdKeyPCDiLai)
        {
            if (_PCDiLaiDelegate.UpdateTrangThaiSuDung(IdKeyPCDiLai) == true)
            {
                _PCDiLaiDelegate.UpdateTrangThaiKhongSuDung(IdNhanVien, IdKeyPCDiLai);
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 0);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemPCDiLai(int IdNhanVien, int IdKeyPCDiLai, int? idPTPCDiLai, PCDiLaiForm form)
        {
            hs_NhanVien_PCDiLai Status;
            if (IdKeyPCDiLai == 0)// thêm mới
            {
                hs_NhanVien_PCDiLai obj = _PCDiLaiDelegate.ConvertToLinq(form);
                obj.IdNhanVien = IdNhanVien;
                // obj.IdKeyPCDiLai = form.ModelPCDiLai.IdKeyPCDiLai;
                Status = _PCDiLaiDelegate.Insert(obj);
                NhanVienForm fromnv = new NhanVienForm();
                if (Status != null)
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 1);
                }
                else
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 0);
                }
                //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaPCDiLai(int IdNhanVien, int IdKeyPCDiLai, int? idPTPCDiLai, PCDiLaiForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            NhanVienForm fromnv = new NhanVienForm();
            hs_NhanVien_PCDiLai obj = _PCDiLaiDelegate.ConvertToLinq(form);
            obj.IdNhanVien = IdNhanVien;
            obj.Id = IdKeyPCDiLai;
            bool result = _PCDiLaiDelegate.UpdatePCDiLai(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult XoaPCDiLai(int IdNhanVien, int IdKeyPCDiLai)
        {
            bool t = _PCDiLaiDelegate.DeletePCDiLai(IdKeyPCDiLai);
            NhanVienForm fromnv = new NhanVienForm();
            if (t == true)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=3&&Status=" + 0);
            }
        }


        public JsonResult EditPCDiLaiAjax(int IdKeyPCDiLai)// IdThongTinNguonThu
        {
            hs_NhanVien_PCDiLai obj = _PCDiLaiDelegate.GetPCDiLaiById(IdKeyPCDiLai.ToString());
            string[] kq = new string[] { obj.Id.ToString(),
                obj.IdPCDiLai!=null? obj.IdPCDiLai.Value.ToString():""};
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion

        // phụ cấp cơm
        #region Phụ cấp cơm
        PCComDelegate _PCComDelegate = new PCComDelegate();
        public ActionResult CapNhatTrangThaiSuDungPCCom(int IdNhanVien, int IdKeyPCCom)
        {
            if (_PCComDelegate.UpdateTrangThaiSuDung(IdKeyPCCom) == true)
            {
                _PCComDelegate.UpdateTrangThaiKhongSuDung(IdNhanVien, IdKeyPCCom);
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 0);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemPCCom(int IdNhanVien, int IdKeyPCCom, int? idPTPCCom, PCComForm form)
        {
            hs_NhanVien_PCCom Status;
            if (IdKeyPCCom == 0)// thêm mới
            {
                hs_NhanVien_PCCom obj = _PCComDelegate.ConvertToLinq(form);
                obj.IdNhanVien = IdNhanVien;
                // obj.IdKeyPCCom = form.ModelPCCom.IdKeyPCCom;
                Status = _PCComDelegate.Insert(obj);
                NhanVienForm fromnv = new NhanVienForm();
                if (Status != null)
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 1);
                }
                else
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 0);
                }
                //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaPCCom(int IdNhanVien, int IdKeyPCCom, int? idPTPCCom, PCComForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            NhanVienForm fromnv = new NhanVienForm();
            hs_NhanVien_PCCom obj = _PCComDelegate.ConvertToLinq(form);
            obj.IdNhanVien = IdNhanVien;
            obj.Id = IdKeyPCCom;
            bool result = _PCComDelegate.UpdatePCCom(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult XoaPCCom(int IdNhanVien, int IdKeyPCCom)
        {
            bool t = _PCComDelegate.DeletePCCom(IdKeyPCCom);
            NhanVienForm fromnv = new NhanVienForm();
            if (t == true)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=4&&Status=" + 0);
            }
        }
        public JsonResult EditPCComAjax(int IdKeyPCCom)// IdThongTinNguonThu
        {
            hs_NhanVien_PCCom obj = _PCComDelegate.GetPCComById(IdKeyPCCom.ToString());
            string[] kq = new string[] { obj.Id.ToString(),
                obj.IdPCCom!=null? obj.IdPCCom.Value.ToString():""};
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion
        // phụ cấp điên thoại
        #region Phụ cấp điên thoại
        PCDienThoaiDelegate _PCDienThoaiDelegate = new PCDienThoaiDelegate();
        public ActionResult CapNhatTrangThaiSuDungPCDienThoai(int IdNhanVien, int IdKeyPCDienThoai)
        {
            if (_PCDienThoaiDelegate.UpdateTrangThaiSuDung(IdKeyPCDienThoai) == true)
            {
                _PCDienThoaiDelegate.UpdateTrangThaiKhongSuDung(IdNhanVien, IdKeyPCDienThoai);
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 0);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemPCDienThoai(int IdNhanVien, int IdKeyPCDienThoai, int? idPTPCDienThoai, PCDienThoaiForm form)
        {
            hs_NhanVien_PCDienThoai Status;
            if (IdKeyPCDienThoai == 0)// thêm mới
            {
                hs_NhanVien_PCDienThoai obj = _PCDienThoaiDelegate.ConvertToLinq(form);
                obj.IdNhanVien = IdNhanVien;
                // obj.IdKeyPCDienThoai = form.ModelPCDienThoai.IdKeyPCDienThoai;
                Status = _PCDienThoaiDelegate.Insert(obj);
                NhanVienForm fromnv = new NhanVienForm();
                if (Status != null)
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 1);
                }
                else
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 0);
                }
                //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaPCDienThoai(int IdNhanVien, int IdKeyPCDienThoai, int? idPTPCDienThoai, PCDienThoaiForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            NhanVienForm fromnv = new NhanVienForm();
            hs_NhanVien_PCDienThoai obj = _PCDienThoaiDelegate.ConvertToLinq(form);
            obj.IdNhanVien = IdNhanVien;
            obj.Id = IdKeyPCDienThoai;
            bool result = _PCDienThoaiDelegate.UpdatePCDienThoai(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult XoaPCDienThoai(int IdNhanVien, int IdKeyPCDienThoai)
        {
            bool t = _PCDienThoaiDelegate.DeletePCDienThoai(IdKeyPCDienThoai);
            NhanVienForm fromnv = new NhanVienForm();
            if (t == true)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=5&&Status=" + 0);
            }
        }
        public JsonResult EditPCDienThoaiAjax(int IdKeyPCDienThoai)// IdThongTinNguonThu
        {
            hs_NhanVien_PCDienThoai obj = _PCDienThoaiDelegate.GetPCDienThoaiById(IdKeyPCDienThoai.ToString());
            string[] kq = new string[] { obj.Id.ToString(),
                obj.IdPCDienThoai!=null? obj.IdPCDienThoai.Value.ToString():""};
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion

        // phụ cấp chuyên cần
        #region Phụ cấp chuyên cần
        PCChuyenCanDelegate _PCChuyenCanDelegate = new PCChuyenCanDelegate();
        public ActionResult CapNhatTrangThaiSuDungPCChuyenCan(int IdNhanVien, int IdKeyPCChuyenCan)
        {
            if (_PCChuyenCanDelegate.UpdateTrangThaiSuDung(IdKeyPCChuyenCan) == true)
            {
                _PCChuyenCanDelegate.UpdateTrangThaiKhongSuDung(IdNhanVien, IdKeyPCChuyenCan);
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 0);
            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemPCChuyenCan(int IdNhanVien, int IdKeyPCChuyenCan, int? idPTPCChuyenCan, PCChuyenCanForm form)
        {
            hs_NhanVien_PCChuyenCan Status;
            if (IdKeyPCChuyenCan == 0)// thêm mới
            {
                hs_NhanVien_PCChuyenCan obj = _PCChuyenCanDelegate.ConvertToLinq(form);
                obj.IdNhanVien = IdNhanVien;
                // obj.IdKeyPCChuyenCan = form.ModelPCChuyenCan.IdKeyPCChuyenCan;
                Status = _PCChuyenCanDelegate.Insert(obj);
                NhanVienForm fromnv = new NhanVienForm();
                if (Status != null)
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 1);
                }
                else
                {
                    return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 0);
                }
                //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + -1);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaPCChuyenCan(int IdNhanVien, int IdKeyPCChuyenCan, int? idPTPCChuyenCan, PCChuyenCanForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            NhanVienForm fromnv = new NhanVienForm();
            hs_NhanVien_PCChuyenCan obj = _PCChuyenCanDelegate.ConvertToLinq(form);
            obj.IdNhanVien = IdNhanVien;
            obj.Id = IdKeyPCChuyenCan;
            bool result = _PCChuyenCanDelegate.UpdatePCChuyenCan(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult XoaPCChuyenCan(int IdNhanVien, int IdKeyPCChuyenCan)
        {
            bool t = _PCChuyenCanDelegate.DeletePCChuyenCan(IdKeyPCChuyenCan);
            NhanVienForm fromnv = new NhanVienForm();
            if (t == true)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + IdNhanVien + "?HidLastTab=6&&Status=" + 0);
            }
        }
        public JsonResult EditPCChuyenCanAjax(int IdKeyPCChuyenCan)// IdThongTinNguonThu
        {
            hs_NhanVien_PCChuyenCan obj = _PCChuyenCanDelegate.GetPCChuyenCanById(IdKeyPCChuyenCan.ToString());
            string[] kq = new string[] { obj.Id.ToString(),
                obj.IdPCChuyenCan!=null? obj.IdPCChuyenCan.Value.ToString():""};
            return Json(kq, JsonRequestBehavior.AllowGet);
        }
        #endregion

        //Thông tin liên quan
        #region Thông tin liên quan
        ThongTinLienQuanDelegate _thongTinLienQuanDelegate = new ThongTinLienQuanDelegate();

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ThemMoiThongTinLienQuan(int Id, ThongTinThemHSNhanVienForm form)
        {
            hs_ThongTinLienQuan Status;
            hs_ThongTinLienQuan obj = _thongTinLienQuanDelegate.ConvertToLinq(form);
            obj.IdNhanVien = Id;
            // obj.IdKeyPCChuyenCan = form.ModelPCChuyenCan.IdKeyPCChuyenCan;
            Status = _thongTinLienQuanDelegate.Insert(obj);
            if (Status != null)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + Id + "?HidLastTab=1&&Status=" + 1);
            }
            else
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + Id + "?HidLastTab=1&&Status=" + 0);
            }
            //    return View("~/Views/QuanLyNhanVien/ThongTinChiTietCuaNhanVien.aspx", fromnv);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SuaThongTinLienQuan(int Id, ThongTinThemHSNhanVienForm form)
        {
            //hs_BenVay obj = _BenVayDelegate.GetById(id);
            hs_ThongTinLienQuan obj = _thongTinLienQuanDelegate.ConvertToLinq(form);
            obj.IdNhanVien = Id;
            bool result = _thongTinLienQuanDelegate.UpdatethongTinLienQuan(obj);
            if (result)
            {
                return Redirect("~/NhanVien/ThongTinChiTiet/" + Id + "?HidLastTab=1&&Status=" + 1);
            }
            else
                return Redirect("~/NhanVien/ThongTinChiTiet/" + Id + "?HidLastTab=1&&Status=" + 0);
            //return Redirect("~/Nghiepvu/ThemMoiHoSo/" + id +"?message="+form.Message);
        }
        #endregion

        #region BACNP

        #region Tạm Ứng

        TamUngDelegate tamUngDelegate = new TamUngDelegate();

        [HttpGet]
        public ActionResult TamUng(int? thang, int? nam, int? idPhongBan, int? idChucVu, string hoTen, int? pageIndex)
        {
            var form = new TamUngForm();
            form.PageIndex = pageIndex > 1 ? pageIndex.Value : 1;
            form.SearchByIdPhongBan = idPhongBan;
            form.SearchByIdChucVu = idChucVu;
            form.SearchByHoTen = !string.IsNullOrEmpty(hoTen) ? hoTen.Trim() : string.Empty;
            var dtToday = DateTime.Today;
            form.ThangTamUng = thang > 0 ? thang.Value : dtToday.Month;
            form.NamTamUng = nam > 0 ? nam.Value : dtToday.Year;
            form.IsFinish = !tamUngDelegate.IsAllowEdit(form.ThangTamUng, form.NamTamUng);

            form.SelectListNhanVien = tamUngDelegate.BuildSelectListNhanVien();
            tamUngDelegate.BuildDanhSachTamUngTheoThang(form);
            if (form.ListTamUng.Count == 0 && form.PageIndex > 1)
            {
                form.PageIndex--;
                tamUngDelegate.BuildDanhSachTamUngTheoThang(form);
            }

            return View("~/Views/QuanLyNhanVien/TamUng.aspx", form);
        }

        [HttpPost]
        public ActionResult AddTamUng(TamUngForm form)
        {
            if (tamUngDelegate.IsAllowEdit(form.ThangTamUng, form.NamTamUng))
            {
                var objTamUng = tamUngDelegate.BuildTamUngLinQ(form);
                objTamUng.IdNhanVien = form.IdNhanVien;
                objTamUng = tamUngDelegate.AddTamUng(objTamUng);

                if (objTamUng != null)
                    form.MessageSuccess = "Lưu Tạm Ứng thành công!";
                else
                    form.MessageError = "Lưu Tạm Ứng thất bại!";
                #region Code chỉ dc tạm ứng 1 lần /tháng
                //if (!tamUngDelegate.IsExistsTamUng(form.IdNhanVien, form.ThangTamUng, form.NamTamUng))
                //{
                //    var objTamUng = tamUngDelegate.BuildTamUngLinQ(form);
                //    objTamUng.IdNhanVien = form.IdNhanVien;
                //    objTamUng = tamUngDelegate.AddTamUng(objTamUng);

                //    if (objTamUng != null)
                //        form.MessageSuccess = "Lưu Tạm Ứng thành công!";
                //    else
                //        form.MessageError = "Lưu Tạm Ứng thất bại!";
                //}
                //else
                //{
                //    form.MessageError = "Nhân viên này đã tạm ứng trước đó! Không thể tạm ứng lần nữa!";
                //}
                #endregion
            }
            else
            {
                form.MessageError = "Bảng Lương tháng này đã hoàn thành! Không thể thêm Tạm Ứng tháng này!";
                form.IsFinish = true;
            }
            if (Request.IsAjaxRequest())
            {
                return Json(form, JsonRequestBehavior.AllowGet);
            }

            form.SelectListNhanVien = tamUngDelegate.BuildSelectListNhanVien();
            tamUngDelegate.BuildDanhSachTamUngTheoThang(form);

            return View("~/Views/QuanLyNhanVien/TamUng.aspx", form);
        }

        [HttpPost]
        public ActionResult UpdateTamUng(TamUngForm form)
        {
            if (tamUngDelegate.IsAllowEdit(form.Id))
            {
                var objTamUng = tamUngDelegate.BuildTamUngLinQ(form);
                objTamUng = tamUngDelegate.UpdateTamUng(objTamUng);

                if (objTamUng != null)
                    form.MessageSuccess = "Cập nhật Tạm Ứng thành công!";
                else
                    form.MessageError = "Cập nhật Tạm Ứng thất bại!";
            }
            else
            {
                form.MessageError = "Tạm Ứng này không được phép cập nhật!";
                form.IsFinish = true;
            }
            if (Request.IsAjaxRequest())
            {
                return Json(form, JsonRequestBehavior.AllowGet);
            }

            form.SelectListNhanVien = tamUngDelegate.BuildSelectListNhanVien();
            tamUngDelegate.BuildDanhSachTamUngTheoThang(form);

            return View("~/Views/QuanLyNhanVien/TamUng.aspx", form);
        }

        /// <summary>
        /// Xóa tạm ứng
        /// </summary>
        /// <param name="id">Id Tạm Ứng</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult DeleteTamUng(int id)
        {
            var form = new TamUngForm();

            if (tamUngDelegate.IsAllowEdit(form.Id))
            {
                if (tamUngDelegate.DeleteTamUng(id))
                    form.MessageSuccess = "Xóa Tạm Ứng thành công!";
                else
                    form.MessageError = "Xóa Tạm Ứng thất bại!";
            }
            else
            {
                form.MessageError = "Tạm Ứng này không được phép xóa!";
                form.IsFinish = true;
            }

            if (Request.IsAjaxRequest())
            {
                return Json(form, JsonRequestBehavior.AllowGet);
            }

            form.SelectListNhanVien = tamUngDelegate.BuildSelectListNhanVien();
            tamUngDelegate.BuildDanhSachTamUngTheoThang(form);

            return View("~/Views/QuanLyNhanVien/TamUng.aspx", form);
        }

        /// <summary>
        /// Lấy thông tin tạm ứng Ajax
        /// </summary>
        /// <param name="id">Id Tạm ứng</param>
        /// <returns></returns>
        public JsonResult GetTamUngAjax(int? id)
        {
            var form = new TamUngForm();
            if (id != null)
            {
                var objTamUng = tamUngDelegate.GetTamUngById(id);
                if (objTamUng != null)
                {
                    form = tamUngDelegate.BuildTamUngForm(objTamUng);
                    form.SoTien = objTamUng.SoTien.ToString("#0");
                }
            }
            else
            {
                form.NgayTamUng = DateTime.Today.ToString("dd/MM/yyyy");
            }
            return Json(form, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region Thôi Việc

        ThoiViecDelegate thoiViecDelegate = new ThoiViecDelegate();

        [HttpPost]
        public JsonResult SaveThoiViec(ThoiViecForm form)
        {
            if (thoiViecDelegate.SaveThoiViec(form))
                return Json(new { isSuccess = true, message = "Lưu thông tin thôi việc thành công!" }, JsonRequestBehavior.AllowGet);
            return Json(new { isSuccess = false, message = "Lưu thông tin thôi việc thất bại!" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Lấy thông tin nhân viên Ajax để thực hiện Thôi Việc
        /// </summary>
        /// <param name="id">Id Nhân Viên</param>
        /// <returns></returns>
        public JsonResult GetNhanVienThoiViecAjax(int id)
        {
            var form = new ThoiViecForm();
            var objNhanVien = thoiViecDelegate.GetNhanVienById(id);
            if (objNhanVien != null)
            {
                form = thoiViecDelegate.BuildThoiViecNhanVien(id);
            }
            
            return Json(form, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #endregion
    }
}
